﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Collections.ObjectModel;
using OpenQA.Selenium.Support.UI;
using System.Threading;

namespace AutoSubmit.Lib
{
    public class ActionPage: IActionPage
    {
        public Login m_login;

        public Submit m_submit;

        public Article m_article;

        public IWebDriver m_driver;

        private String m_errMessage = "";

        protected SubmitStatus m_status = SubmitStatus.NEW;

        public ActionPage()
        {
            m_driver = new FirefoxDriver();
        }

        public bool Login()
        {
            m_status = SubmitStatus.LOGINING;

            if (m_login == null || 
                m_login.m_loginURL == null || m_login.m_loginURL.Length == 0 ||
                m_login.m_userName == null || m_login.m_userName.Length==0 ||
                m_login.m_password == null || m_login.m_password.Length==0 ||
                m_login.m_userNameXPath == null || m_login.m_userNameXPath.Length==0 ||
                m_login.m_passwordXPath == null || m_login.m_passwordXPath.Length==0 ||
                m_login.m_submitXPath == null || m_login.m_submitXPath.Length == 0)
            {
                m_errMessage = "Error Config";
                m_status = SubmitStatus.ERROR_LOGIN;
                return false;
            }

            try
            {
                vLogin();
            }
            catch (System.Exception ex)
            {
                m_status = SubmitStatus.ERROR_LOGIN;
                m_errMessage += ex.Message;
                return false;
            }

            return true;
        }

        public bool Submit()
        {
            m_status = SubmitStatus.SUBMITTING;
            try
            {
                vSubmit();
                vCheckErrorBox();
            }
            catch (System.Exception ex)
            {
                m_status = SubmitStatus.ERROR_SUBMIT;
                m_errMessage = ex.Message;
                return false;
            }

            m_status = SubmitStatus.SUCCESS;
            return true;
        }

        public String GetArticleURL()
        {
            try
            {
                if (m_status != SubmitStatus.SUCCESS)
                {
                    throw new Exception("Status is not SUCCESS!");
                }

                return vGetArticleURL();
            }
            catch (System.Exception ex)
            {
                m_status = SubmitStatus.ERROR_GET_ARTICLE_URL;
                m_errMessage = ex.Message;
            }

            return "";
        }

        public SubmitStatus GetStatus()
        {
            return m_status;
        }

        public String GetErrMessage()
        {
            return m_errMessage;
        }

        public virtual void vSubmit()
        {
            if (m_submit.m_url.Length > 0)
            {
                m_driver.Navigate().GoToUrl(m_submit.m_url);
            }

            // Select main Category
            if(m_submit.m_isCategorySelect)
            {
                if (m_submit.m_mainCategoryXPath.Length > 0)
                {
                    SelectElement catagoryMain = new SelectElement(WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_mainCategoryXPath), 5));
                    catagoryMain.SelectByText(m_submit.m_mainCategory);
                }
                
                if (m_submit.m_subCatagoryXPath.Length > 0)
                {
                    SelectElement catagorySub = new SelectElement(WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_subCatagoryXPath), 5));
                    Thread.Sleep(5000);
                    catagorySub.SelectByText(m_submit.m_subCatagory);
                }
                
            }
            else
            {
                IWebElement catagoryMain = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_mainCategoryXPath), 5);
                catagoryMain.Click();
            }
            

            // Set title
            if (m_submit.m_titleXPath.Length > 0)
            {
                IWebElement titleInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_titleXPath), 5);
                titleInput.SendKeys(m_submit.m_title);
            }

            // Set article body
            if (m_submit.m_bodyXPath.Length > 0)
            {
                IWebElement bodyInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_bodyXPath), 5);
                bodyInput.SendKeys(m_submit.m_body);
            }

            // Set bio
            if (m_submit.m_bioXPath.Length > 0)
            {
                IWebElement bioInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_bioXPath), 5);
                bioInput.Clear();
                bioInput.SendKeys(m_submit.m_bio);
            }

            // Set key words
            if (m_submit.m_keywordsXPath.Length > 0)
            {
                IWebElement keywordsInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_keywordsXPath), 5);
                keywordsInput.SendKeys(m_submit.m_keywords);
            }

            // Set summary
            if (m_submit.m_summaryXPath.Length > 0)
            {
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_summaryXPath), 5);
                summaryInput.SendKeys(m_submit.m_summary);
            }

            // Set author
            if (m_submit.m_authorXPath.Length > 0)
            {
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_authorXPath), 5);
                summaryInput.Clear();
                summaryInput.SendKeys(m_submit.m_author);
            }

            // Set bio plain
            if (m_submit.m_bioPlainXPath.Length > 0)
            {
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_bioPlainXPath), 5);
                summaryInput.SendKeys(m_submit.m_bioPlain);
            }

            // Set Web URL
            if (m_submit.m_webURLXPath.Length > 0)
            {
                IWebElement webURLInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_webURLXPath), 5);
                webURLInput.SendKeys(m_submit.m_webURL);
            }
            

            // Set primary Search Phrase
            if (m_submit.m_primarySearchPhraseXPath.Length > 0)
            {
                IWebElement phraseInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_primarySearchPhraseXPath), 5);
                phraseInput.SendKeys(m_submit.m_primarySearchPhrase);
            }

            // Set TOS
            if (m_submit.m_tosXPath.Length > 0)
            {
                IWebElement phraseInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_tosXPath), 5);
                phraseInput.Click();
            }

            // Submit
            IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_submitButtonXPath), 5);
            if (m_submit.m_isClick) 
                submitButton.Click();
            else
                submitButton.Submit();
        }

        public virtual void vCheckErrorBox()
        {
            IWebElement errBox = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_errBoxXPath), 5);
            if (errBox != null)
            {
                throw new Exception(errBox.Text);
            }
        }

        public virtual String vGetArticleURL()
        {
            if (m_status != SubmitStatus.SUCCESS)
            {
                return "";
            }

            if (m_article.m_url.Length > 0)
            {
                m_driver.Navigate().GoToUrl(m_article.m_url);
          }

            // find new article
            ReadOnlyCollection<IWebElement> hrefs = WebDriverExtension.FindElements(m_driver, By.XPath(m_article.m_articlesListXPath), 5);
            foreach (IWebElement ele in hrefs)
            {
                if (ele.Text.Contains(m_article.m_title))
                    return ele.GetAttribute("href");
            }

            return "";
        }

        public virtual void vLogin()
        {
            m_driver.Navigate().GoToUrl(m_login.m_loginURL);

            IWebElement userInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_login.m_userNameXPath), 5);// m_driver.FindElement(By.XPath("//label/input[@name='email']"));
            userInput.SendKeys(m_login.m_userName);

            IWebElement passwordInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_login.m_passwordXPath), 5);  // m_driver.FindElement(By.XPath("//label/input[@name='password']"));
            passwordInput.SendKeys(m_login.m_password);

            IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.XPath(m_login.m_submitXPath), 5);  //m_driver.FindElement(By.XPath("//form/input[@value='Login']"));
            if (m_login.m_isClick)
            {
                submitButton.Click();
            }
            else
            {
                submitButton.Submit();
            }
        }
    }

}
